package com.blast.comicsisland.activity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blast.comicsisland.R;
import com.blast.comicsisland.WriteToSD;
import com.blast.comicsisland.fragment.BookShelf;
import com.blast.comicsisland.fragment.BookStoreFragment;
import com.blast.comicsisland.fragment.CommunityFragment;
import com.blast.comicsisland.fragment.ProfileFragment;
import com.blast.comicsisland.fragment.SearchFragment;


/**
 * 主界面 下方RadioButton应包含五项 书架 书城 猫脸 圈子 我的
 * <p/>
 * 默认选中为书城上方为ViewPager 帧布局 FragmentViewPagerAdapter
 */
public class MainActivity extends ActionBarActivity implements View.OnClickListener{

    private RadioGroup group;
    private RadioButton btnBookCase; //书架按钮
    private RadioButton btnBookStore; //书城按钮
    private RadioButton btnDiscover; //发现按钮 中间的猫
    private RadioButton btnCircle; //圈子按钮
    private RadioButton btnMime; //我的设置按钮
    private FrameLayout frameLayout;
    private BookStoreFragment bookStoreFragment; //书城Fragment
    private ProfileFragment profileFragment; //我的Fragment
    private BookShelf bookShelf; //书架Fragment
    private CommunityFragment communityFragment; //圈子Fragment
    private SearchFragment searchFragment; //搜索Fragment
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private AlertDialog alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /**
         *把assets下的漫画岛logo写到SDCard下，便于获取路径
         */
        WriteToSD writeToSD = new WriteToSD(this);
        writeToSD.toSDCard();

        group = (RadioGroup) findViewById(R.id.main_activity_radio_group);
        frameLayout = (FrameLayout) findViewById(R.id.main_frame_layout);
        final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) frameLayout.getLayoutParams();

        btnBookCase = (RadioButton) findViewById(R.id.main_bookcase_radio_button);
        btnBookStore = (RadioButton) findViewById(R.id.main_bookstore_radio_button);
        btnDiscover = (RadioButton) findViewById(R.id.main_discover_radio_button);
        btnCircle = (RadioButton) findViewById(R.id.main_circle_radio_button);
        btnMime = (RadioButton) findViewById(R.id.main_mine_radio_button);

        alertDialog = new AlertDialog.Builder(this).create();

        btnBookCase.setOnClickListener(this);
        btnBookStore.setOnClickListener(this);
        btnDiscover.setOnClickListener(this);
        btnCircle.setOnClickListener(this);
        btnMime.setOnClickListener(this);

        //计算FrameLayout的大小让其与RadioGroup中的RadioButton同高
        final ViewTreeObserver observer = btnBookStore.getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                int groupTop = group.getTop();
                int top = btnBookStore.getTop();
                int sumTop = groupTop + top;
                int right = btnMime.getRight();
                params.width = right;
                params.height = sumTop;

                frameLayout.setLayoutParams(params);

                btnBookStore.getViewTreeObserver().removeOnPreDrawListener(this);

                return true;
            }
        });

        btnBookStore.setChecked(true);
        if (bookStoreFragment == null) {
            bookStoreFragment = new BookStoreFragment();
            FragmentManager manager = getSupportFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.add(R.id.main_frame_layout, bookStoreFragment);
            transaction.commit();
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.main_bookcase_radio_button:
                if (bookShelf == null) {
                    bookShelf = new BookShelf();
                    manager = getSupportFragmentManager();
                    transaction = manager.beginTransaction();
                    transaction.add(R.id.main_frame_layout, bookShelf);
                    transaction.commit();
                }
                manager = getSupportFragmentManager();
                transaction = manager.beginTransaction();
                transaction.show(bookShelf);
                if (bookStoreFragment != null&& !bookStoreFragment.isHidden()) {
                    transaction.hide(bookStoreFragment);
                } else if (searchFragment != null&& !searchFragment.isHidden()) {
                    transaction.hide(searchFragment);
                } else if (communityFragment != null&& !communityFragment.isHidden()) {
                    transaction.hide(communityFragment);
                } else if (profileFragment != null&& !profileFragment.isHidden()) {
                    transaction.hide(profileFragment);
                }
                transaction.commit();

                break;
            case R.id.main_bookstore_radio_button:
                if (bookStoreFragment == null) {
                    bookStoreFragment = new BookStoreFragment();
                    manager = getSupportFragmentManager();
                    FragmentTransaction transaction = manager.beginTransaction();
                    transaction.add(R.id.main_frame_layout, bookStoreFragment);
                    transaction.commit();
                }
                manager = getSupportFragmentManager();
                transaction = manager.beginTransaction();
                transaction.show(bookStoreFragment);
                if (bookShelf != null&& !bookShelf.isHidden()) {
                    transaction.hide(bookShelf);
                } else if (searchFragment != null&& !searchFragment.isHidden()) {
                    transaction.hide(searchFragment);
                } else if (communityFragment != null&& !communityFragment.isHidden()) {
                    transaction.hide(communityFragment);
                } else if (profileFragment != null&& !profileFragment.isHidden()) {
                    transaction.hide(profileFragment);
                }
                transaction.commit();

                break;
            case R.id.main_discover_radio_button:
                if (searchFragment == null) {
                    searchFragment = new SearchFragment();
                    manager = getSupportFragmentManager();
                    FragmentTransaction transaction = manager.beginTransaction();
                    transaction.add(R.id.main_frame_layout, searchFragment);
                    transaction.commit();
                }
                manager = getSupportFragmentManager();
                transaction = manager.beginTransaction();
                transaction.show(searchFragment);
                if (bookShelf != null && !bookShelf.isHidden()) {
                    transaction.hide(bookShelf);
                } else if (bookStoreFragment != null&& !bookStoreFragment.isHidden()) {
                    transaction.hide(bookStoreFragment);
                } else if (communityFragment != null&& !communityFragment.isHidden()) {
                    transaction.hide(communityFragment);
                } else if (profileFragment != null && !profileFragment.isHidden()) {
                    transaction.hide(profileFragment);
                }
                transaction.commit();

                break;
            case R.id.main_circle_radio_button:
                if (communityFragment == null) {
                    communityFragment = new CommunityFragment();
                    manager = getSupportFragmentManager();
                    FragmentTransaction transaction = manager.beginTransaction();
                    transaction.add(R.id.main_frame_layout, communityFragment);
                    transaction.commit();
                }
                manager = getSupportFragmentManager();
                transaction = manager.beginTransaction();
                transaction.show(communityFragment);
                if (bookShelf != null && !bookShelf.isHidden()) {
                    transaction.hide(bookShelf);
                } else if (bookStoreFragment != null && !bookStoreFragment.isHidden()) {
                    transaction.hide(bookStoreFragment);
                } else if (searchFragment != null && !searchFragment.isHidden()) {
                    transaction.hide(searchFragment);
                } else if (profileFragment != null && !profileFragment.isHidden()) {
                    transaction.hide(profileFragment);
                }
                transaction.commit();

                break;
            case R.id.main_mine_radio_button:
                if (profileFragment == null) {
                    profileFragment = new ProfileFragment();
                    manager = getSupportFragmentManager();
                    FragmentTransaction transaction = manager.beginTransaction();
                    transaction.add(R.id.main_frame_layout, profileFragment);
                    transaction.commit();
                }
                manager = getSupportFragmentManager();
                transaction = manager.beginTransaction();
                transaction.show(profileFragment);
                if (bookShelf != null&& !bookShelf.isHidden()) {
                    transaction.hide(bookShelf);
                } else if (bookStoreFragment != null && !bookStoreFragment.isHidden()) {
                    transaction.hide(bookStoreFragment);
                } else if (searchFragment != null && !searchFragment.isHidden()) {
                    transaction.hide(searchFragment);
                } else if (communityFragment != null && !communityFragment.isHidden()) {
                    transaction.hide(communityFragment);
                }
                transaction.commit();
                break;

            case R.id.btnCancelBack:
                alertDialog.dismiss();
                break;
            case R.id.btnSureBacks:
                finish();
                break;
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if (alertDialog == null){
                alertDialog = new AlertDialog.Builder(this).create();
            }

            View view = LayoutInflater.from(this).inflate(R.layout.back_application, null);

            TextView textCancel = (TextView) view.findViewById(R.id.btnCancelBack);
            TextView textSure = (TextView) view.findViewById(R.id.btnSureBacks);
            textCancel.setOnClickListener(this);
            textSure.setOnClickListener(this);
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();
            alertDialog.setContentView(view);
        }

        return false;
    }

    @Override
    protected void onDestroy() {
        alertDialog.dismiss();
        super.onDestroy();
    }
}
